/*******************************************************
*  Team name: 
*  Class involve:
*  Version, Author, History of modification:
*  Ver 1.0 , Ada Ko
*  		- added constructor, variables needed
*  Ver 1.0, Fred 
*  		- added setDisplay, getMagicToolDesp functions
*  Ver 1.1, Ada Ko
*  		-updated complete removal of used MT (actionPerformed method)
*   	-add move Forward checking
*   	-add setBigMTValue(String label)  for setting the Big Picture of the panel
*  Ver 1.2, Fred
*  		-add other MagicTool Card like Bonus and Skip Card
*  Ver 1.3, Fred
*  		-actionPerformed Edited, make MT Enable
*******************************************************/

package boundary;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;
import javax.swing.JLabel;

import control.BoardEngine;
import control.UseMTController;
import java.awt.Font;



/*Things to be done:
 *  1. In roundWindow: do checking on bonus and skip card , don't allow users to use these unless they are in the event/SA panel
 *  2. fine-tune the layout of UseMTWindow
 * */
public class UseMTWindow extends CardWindow implements ActionListener{

	/**
	 * Create the panel.
	 */
	JPanel CardPanel;
	CardLayout cl;
	JLabel info;
	JButton MTBackBtn;
	JButton MTConfirmBtn;
	JButton MTUseBtn;
	JButton MTCancelBtn;
	JLabel confirmmsg;
	JLabel desp;
	JLabel lblUseMtWindow;
	JLabel MTLabel; // the enlarged MT Card
	String msg ="";
	String hihihi="";
	int currentPlayer;
	int MTID;
	String currentMTType;

	UseMTController UseMTController;
	public UseMTWindow(CardLayout cl, RoundWindow RW, UseMTController UseMTControl)
	{
	
		super(cl,RW);	
		setBounds(0,0,800,568);
		lblUseMtWindow = new JLabel("Use MT Window");
		lblUseMtWindow.setBounds(400, 50, 431, 100);
		desp = new JLabel("DESP");
		desp.setBounds(400, 200, 431, 100);
		MTUseBtn = new JButton("Use");
		MTUseBtn.setFont(new Font("Arial", Font.PLAIN, 35));
		MTUseBtn.setBounds(310, 470, 110, 50);
		MTBackBtn = new JButton("Back");
		MTBackBtn.setFont(new Font("Arial", Font.PLAIN, 35));

		
		this.UseMTController=UseMTControl;
		this.UseMTController.initUseMTWindow(this);	
		
		MTBackBtn.setBounds(460, 470, 130, 50);
		MTBackBtn.addActionListener(this);
		
		info=new JLabel();

		confirmmsg = new JLabel("Do you confirm?");
		MTUseBtn.addActionListener(this);
		
		MTLabel = new JLabel();
		MTLabel.setBounds(50,50, 270, 360);
		super.CardPanel.add(MTUseBtn);
		super.CardPanel.add(desp);
		super.CardPanel.add(lblUseMtWindow);
		super.CardPanel.add(MTLabel);
		super.CardPanel.add(MTBackBtn);
		super.CardPanel.add(info);
		
	}

	public void setPlayerInfo(int currentPlayer, int MTID){
		
		this.currentPlayer=currentPlayer;
		this.MTID = MTID;

	}
	
	public void setDisplay()
	{		
		//string MTType = UseMTController.boardEngine.board.getPlayerMT(this.currentPlayer);
		desp.setText(getMagicToolDesp());
		super.roundWindow.printCardPanel("UseMTPanel");
		System.out.println("This is Description: "+ getMagicToolDesp());
	}
	
	public String getMagicToolDesp()
	{
		
		String desp = UseMTController.getMagicToolDesp(this.currentPlayer, this.MTID);
		return desp;
		
	}


	@Override
	public void process() {
		// TODO Auto-generated method stub
		
	}

	public void setLabelValue(String label)
	{
		info.setText(label);
		System.out.println("UseMTWindow- setlabelValue- "+label);
	
	}
	
	
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == MTUseBtn)
		{
			msg = confirmmsg.getText();
			int n= JOptionPane.showConfirmDialog(null, msg,"Use this Card?", JOptionPane.YES_NO_OPTION);
			
			// please finish the other 2 cards' effects
			if(n==JOptionPane.YES_OPTION)
			{
				this.UseMTController.removeUsedMT(this.currentPlayer, this.MTID);
				useCard(this.currentMTType);
				super.roundWindow.checkEndBtnState();
			}
			else
			{
				printTheRightPanel();
				super.roundWindow.disableEndBtn();
			}
		}
		else if(e.getSource() == MTBackBtn)
		{
			printTheRightPanel();
			super.roundWindow.disableEndBtn();
		}	
		
	}
	public void printTheRightPanel(){
		
		
		if(this.UseMTController.getAtPanel()=="Main"){
			super.roundWindow.printBoardPanel();
			super.roundWindow.printMTPanel();
		}
		else {
		
			super.roundWindow.boardEngine.setEventSAMTVisible();
			super.roundWindow.printCardPanel(this.UseMTController.getAtPanel());
		}
		
	}
	public void useCard(String currentMTType){
		
		
		if(currentMTType=="Forward")
			useForwardCard();
		else if(currentMTType=="Bonus")
			useBonusCard();
		else
			useSkipCard();
	}
	
	
	public void useForwardCard(){
		
		System.out.println("useMTWindow- Forward clicked.");
		this.roundWindow.movePlayer(this.currentPlayer, 2);
		printTheRightPanel();
		
		
	}
	
	public void useBonusCard()
	{
		
		System.out.println("useMTWindow- Bonus clicked.");
		this.UseMTController.addBonus();
		printTheRightPanel();
		
	}
	
	public void useSkipCard()
	{
		
		System.out.println("useMTWindow- Skip clicked.");
		super.roundWindow.printBoardPanel();
		super.roundWindow.printMTPanel();
		
	}

	public void setBigMTValue(String label) 
	{
		this.currentMTType=label;
		Icon temp;
		if(label=="Forward")
			temp=new ImageIcon("images\\MT_Flight.gif");
		else if(label=="Skip")
			temp=new ImageIcon("images\\MT_SlingShot.gif");
		else if(label=="Bonus")
			temp= new ImageIcon("images\\MT_Crown.gif");
		else
			temp=new ImageIcon("images\\Role_Unknown.gif");
		
		MTLabel.setIcon(temp);
		MTLabel.setLayout(null);
	}
}
